package com.linkage.jz.bankruptcy.activity.Vote;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.Vote.ProVoteVotersGroupsAdapter;
import com.linkage.jz.bankruptcy.adapter.Vote.ProVoteVotersNoGroupsAdapter;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoters;
import com.linkage.seapeak.activity.message.MultiSelectActivity;
import com.linkage.seapeak.activity.vote.CreditorMultiSelectActivity;
import com.linkage.seapeak.activity.vote.VoteTurnMoneyActivity;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.net.ResultCallBack;
import com.linkage.seapeak.net.VoteNetRequest;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.pojo.VoteGroup;
import com.seapeak.recyclebundle.AYSwipeRecyclerView;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 新建选择表决成员 无分组
 * 返回值为 List<CrediterUser> 对应键名为 result
 *
 */
public class ProVoteVotersNoGroupsActivity extends BaseWhiteActivity {

    private static final int REQUEST_CODE_MEMBER = 0xf1;
    private static final int REQUEST_CODE_MONEY = 0xf2;
    private String projectId;

    private AYSwipeRecyclerView mRecyclerView;
    private RelativeLayout emptyView;
    private View bottomLay;
    private ProVoteVotersGroupsAdapter mDataAdapter;

    private ArrayList mData;
    private int moneyIndex = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_vote_voters_no_group, "表决人员 （0人）");
        projectId = getIntent().getStringExtra("projectId");
        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_MEMBER && resultCode == RESULT_OK && data != null) {
            List<CrediterUser> userList = (List<CrediterUser>) data.getSerializableExtra("userList");
            mData.clear();
            mData.addAll(userList);
            notifyDataChanged();
        } else if (requestCode == REQUEST_CODE_MONEY && resultCode == RESULT_OK && data != null) {
            CrediterUser user = (CrediterUser) mData.get(moneyIndex);
            user.setVoteMoney(data.getDoubleExtra("money", 0d));
            notifyDataChanged();
        }
    }

    /**
     * 重新计算，刷新
     */
    public void notifyDataChanged() {
        setTitle(mData.size() == 0 ? "表决人员（0人）" : "表决人员(" + mData.size() + "人)");
        mDataAdapter.notifyDataSetChanged();
        emptyView.setVisibility(mData.size() == 0 ? View.VISIBLE : View.GONE);
        bottomLay.setVisibility(mData.size() != 0 ? View.VISIBLE : View.GONE);
    }

    public void init() {
        mRecyclerView = findViewById(R.id.list);
        emptyView = findViewById(R.id.empty);
        bottomLay = findViewById(R.id.bottom_lay);

        mData = new ArrayList<>();
        mDataAdapter = new ProVoteVotersGroupsAdapter(ProVoteVotersNoGroupsActivity.this, mData);
        mRecyclerView.setMode(AYSwipeRecyclerView.SwipeType.DISABLE);
        mRecyclerView.setShowEmpty(false);
        mRecyclerView.setAdapter(mDataAdapter);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProVoteVotersNoGroupsActivity.this, CreditorMultiSelectActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("type", "2");
                startActivityForResult(intent, REQUEST_CODE_MEMBER);
            }
        });

        mDataAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position, RecyclerView.ViewHolder viewHolder) {
                switch (v.getId()) {
                    case R.id.voter_change:
                        moneyIndex = position;
                        Intent intent = new Intent(ProVoteVotersNoGroupsActivity.this, VoteTurnMoneyActivity.class);
                        intent.putExtra("money", ((CrediterUser) mData.get(moneyIndex)).getVoteMoney());
                        startActivityForResult(intent, REQUEST_CODE_MONEY);
                        break;
                    case R.id.btnDelete:
                        mData.remove(position);
                        break;
                }
                notifyDataChanged();
            }
        });

        addMenu(new MenuAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("result", (Serializable) mData);
                setResult(RESULT_OK, intent);
                finish();
            }
        }, "完成", 0));
        findViewById(R.id.add_member).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> ids = new ArrayList<>();
                for (CrediterUser user : ((List<CrediterUser>)mData)){
                    ids.add(user.getCreditorid());
                }
                Intent intent = new Intent(ProVoteVotersNoGroupsActivity.this, CreditorMultiSelectActivity.class);
                intent.putExtra("projectId", projectId);
                intent.putExtra("type", "2");
                intent.putExtra("alreadySelectIds", (Serializable) ids);
                startActivityForResult(intent, REQUEST_CODE_MEMBER);
            }
        });
        findViewById(R.id.create_ids).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgress();
//                VoteNetRequest.getMeetingNumber(getUserToken(), (List<CrediterUser>) mData, new ResultCallBack<JsonArray>() {
//                    @Override
//                    public void onSuccess(JsonArray result) {
//                        Log.i(TAG, "getMeetingNumber onSuccess: " + result.toString());
//                        for (int i = 0;i < result.size();i++){
//                            JsonObject jsonObject = result.get(i).getAsJsonObject();
//                            String mId = jsonObject.get("creditorId").getAsString();
//                            for(int k = 0;k < mData.size();k++) {
//                                String targetId = ((ArrayList<CrediterUser>) mData).get(k).getCreditorid();
//                                if (mId.equals(targetId)) {
//                                    int meetingNumber = jsonObject.get("meetingNumber").getAsInt();
//                                    ((ArrayList<CrediterUser>) mData).get(k).setMeetingNumber(meetingNumber);
//                                    continue;
//                                }
//                            }
//                        }
//                        hideProgress();
//                        notifyDataChanged();
//                    }
//
//                    @Override
//                    public void onFailed(String msg) {
//                        showToast(msg);
//                        hideProgress();
//                    }
//                });
            }
        });
    }

}
